package com.wanxi.servlet.user;

import com.alibaba.fastjson.JSONObject;
import com.wanxi.dto.ResultDTO;
import com.wanxi.vo.UserVO;
import com.wanxi.service.UserService;
import com.wanxi.service.impl.UserServiceImpl;
import com.wanxi.util.Tool;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

@WebServlet("/back/user/all")
public class UserFindAllServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 1.乱码
        req.setCharacterEncoding(StandardCharsets.UTF_8.toString());
        resp.setCharacterEncoding(StandardCharsets.UTF_8.toString());
//        2.得到前端的值
        String page = req.getParameter("page");
        String limit = req.getParameter("limit");
        String start = req.getParameter("start");
        String end = req.getParameter("end");
        String hobby = req.getParameter("hobby");
        String sex = req.getParameter("sex");
        String username = req.getParameter("username");
        String email = req.getParameter("email");
        String enable = req.getParameter("enable");

        ResultDTO resultDTO = null;
//        3.封装VO数据
        UserVO userVO = new UserVO();
        userVO.setPage(Tool.strToPage(page));
        userVO.setLimit(Tool.strToLimit(limit));
        userVO.setStartBirthday(Tool.nullToStr(start));
        userVO.setEndBirthday(Tool.nullToStr(end));
        userVO.setHobby(Tool.nullToStr(hobby));
        userVO.setSex(Tool.nullToStr(sex));
        userVO.setUsername(Tool.nullToStr(username));
        userVO.setEmail(Tool.nullToStr(email));
        userVO.setEnable(Tool.strToInt(enable));

//        4.调用服务逻辑层
        UserService userService = new UserServiceImpl();
//        5.得到返回的结果
        resultDTO = userService.findAll(userVO);


//        6.返回给前端

        resp.getWriter().println(JSONObject.toJSONString(resultDTO));
    }
}
